using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using EzDesk.Apps.Model.Attendance;
using EzDesk.Apps.ServiceImpl.Attendance.Repositories.Interface;
using EzDesk.Utility;

namespace EzDesk.Apps.ServiceImpl.Attendance.Repositories
{
    public partial class EvaluationModifyApplicationRepository : IEvaluationModifyApplicationRepository
    {
        EzDeskAttendanceDataContext _db;

        public EvaluationModifyApplicationRepository(EzDeskAttendanceDataContext db)
        {
            this._db = db;
        }
		
		#region IRepository<AttendanceEvaluationChangeInfo> Members

        public Guid Add(EvaluationModifyApplicaiton model, bool saveChanges)
        {   
            _db.ez_EvaluationModifyApplications.InsertOnSubmit(
                new ez_EvaluationModifyApplication()
                {
					ApplyId = model.Id,
					
                    ApplyUserId = model.ApplyUserId,
					ApplyOn = model.ApplyOn,						
					ApplyText = model.ApplyText,						
					Date = model.Date,						
					State = (int)model.State,
                });
            if (saveChanges)
            {
                this.SaveChanges();
            }
            return model.Id;
        }

        public EvaluationModifyApplicaiton GetById(Guid id)
        {
            EvaluationModifyApplicaiton info = null;
            info = _db.ez_EvaluationModifyApplications.Where(p => p.ApplyId == id)
                .Select(p => new EvaluationModifyApplicaiton()
                                    {
                                        Id = p.ApplyId,

                                        ApplyUserId = p.ApplyUserId,
                                        ApplyOn = p.ApplyOn,						
                                        ApplyText = p.ApplyText,						
                                        Date = p.Date,						
                                        State = (ApplyState)p.State,	
					                    FeedId = p.FeedId.GetValueOrDefault(Guid.Empty)
                                    })
                .SingleOrDefault();

            return info;
        }

        public IQueryable<EvaluationModifyApplicaiton> GetAll()
        {
            return _db.ez_EvaluationModifyApplications.Select(
                p => new EvaluationModifyApplicaiton()
                {
					Id = p.ApplyId,

                    ApplyUserId = p.ApplyUserId,
					ApplyOn = p.ApplyOn,						
					ApplyText = p.ApplyText,						
					Date = p.Date,						
					State = (ApplyState)p.State,		
				    FeedId = p.FeedId.GetValueOrDefault(Guid.Empty),
                }
            );
        }

        public void Delete(Guid id, bool saveChanges)
        {
            ez_EvaluationModifyApplication entity = _db.ez_EvaluationModifyApplications.Where(p => p.ApplyId == id).SingleOrDefault();
            if (entity != null)
            {
                _db.ez_EvaluationModifyApplications.DeleteOnSubmit(entity);
            }
            if (saveChanges)
            {
                SaveChanges();
            }
        }

        public void Update(EvaluationModifyApplicaiton model, bool saveChanges)
        {
            var entity = _db.ez_EvaluationModifyApplications.Where(p => p.ApplyId == model.Id)
                .SingleOrDefault();

            entity.ApplyUserId = model.ApplyUserId;
			entity.ApplyOn = model.ApplyOn;
			entity.ApplyText = model.ApplyText;
			entity.Date = model.Date;
			entity.State = (int)model.State;
            entity.FeedId = model.FeedId;

            if (saveChanges)
            {
                SaveChanges();
            }
        }

        public void SaveChanges()
        {
            _db.SubmitChanges();
        }
		#endregion
    }
}
